<?php
/**
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
class PostController extends Controller {

    public $layout = 'column1';

    public function actionIndex() {
        //get all the post
        $criteria = new CDbCriteria();
        $criteria->condition = "post_type = '{$this->module->postType}'";
        $criteria->order = 'created DESC';

        //pagination
        $pages = new CPagination();
        $pages->setItemCount(Post::model()->count($criteria));
        $pages->setPageSize($this->module->pageSize);

        //apply
        $pages->applyLimit($criteria);

        $models = Post::model()->findAll($criteria);
        //render
        $this->render('index', array(
            'models' => $models,
            'pages' => $pages
        ));
    }

    /**
     * render view
     */
    public function actionView($alias){
        //get model with post type
        $model = Post::model()->find('`post_type` = :type AND `alias` = :alias', array(
            ':type' => $this->module->postType,
            ':alias' => $alias
        ));

        if(!$model){
            //404
            throw new CHttpException(404, 'Page was not exists!');
        }

        //seo support
        $this->description = $model->meta_description;
        $this->keywords = $model->meta_keywords;

        //render layout
        $this->render('view', array(
            'model' => $model
        ));
    }

}